package com.example.mes_common.utils;

import com.example.mes_common.result.JsonResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.AuthorizationServiceException;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.csrf.CsrfException;

public class AuthExceptionUtil {
    public static JsonResult getErrMsgByExceptionType(AuthenticationException e) {
        if (e instanceof LockedException) {
            return ResultTool.fail(1100, "账户被锁定，请联系管理员!");
        } else if (e instanceof CredentialsExpiredException) {
            return ResultTool.fail(1105,"用户名或者密码输入错误!");
        }else if (e instanceof InsufficientAuthenticationException) {
            return ResultTool.fail(403,"权限不足请登录!");
        } else if (e instanceof AccountExpiredException) {
            return ResultTool.fail(1101, "账户过期，请联系管理员!");
        } else if (e instanceof DisabledException) {
            return ResultTool.fail(1102, ("账户被禁用，请联系管理员!"));
        } else if (e instanceof BadCredentialsException) {
            return ResultTool.fail(1105, "用户名或者密码输入错误!");
        }else if (e instanceof AuthenticationServiceException) {
            return ResultTool.fail(1106, "认证失败，请重试!");
        }

        return ResultTool.fail(1200, e.getMessage());
    }
    public static JsonResult getErrMsgByExceptionType(AccessDeniedException e) {
        if (e instanceof CsrfException) {
            return ResultTool.fail(-1001, "非法访问跨域请求异常!");
        } else if (e instanceof AuthorizationServiceException) {
            return ResultTool.fail(1101, "认证服务异常请重试!");
        }else if (e instanceof AccessDeniedException) {
            return ResultTool.fail(4003, "权限不足不允许访问!");
        }

        return ResultTool.fail(1200, e.getMessage());
    }

}
